package com.ruoyi.system.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Collect;
import com.ruoyi.system.mapper.UserMapper;
import com.ruoyi.system.service.ICollectService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 用户收藏Controller
 *
 * @author AousVolad
 * @date 2024-06-27
 */
@RestController
@RequestMapping("/collect/collect")
public class CollectController extends BaseController {
    @Autowired
    private ICollectService collectService;


/*
    @Autowired
    private UserMapper userMapper;
    LoginUser loginUser = userMapper.selectUserByUsername("123123");

*/

    /**
     * 查询用户收藏列表
     */
    @PreAuthorize("@ss.hasPermi('collect:collect:list')")
    @GetMapping("/list")
    public TableDataInfo list(Collect collect) {
        startPage();

        List<Collect> list = collectService.selectCollectList(collect);
        return getDataTable(list);
    }

    /*
     * TODO 用户收藏热点记录统计
     */

    /**
     * 查询 收藏 记录
     */
    @PreAuthorize("@ss.hasPermi('collect:collect:list')")
    @GetMapping("/one")
    public AjaxResult getNewsCollect(
            @Param("userId") Long userId,
            @Param("articleId") Long articleId,
            @Param("videosId") Long videosId) {
        return success(collectService.getNewsCollect(userId, articleId, videosId));
    }

    /**
     * 导出用户收藏列表
     */
    @PreAuthorize("@ss.hasPermi('collect:collect:export')")
    @Log(title = "用户收藏", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Collect collect) {
        List<Collect> list = collectService.selectCollectList(collect);
        ExcelUtil<Collect> util = new ExcelUtil<Collect>(Collect.class);
        util.exportExcel(response, list, "用户收藏数据");
    }

    /**
     * 获取用户收藏详细信息
     */
    @PreAuthorize("@ss.hasPermi('collect:collect:query')")
    @GetMapping(value = "/{favoriteId}")
    public AjaxResult getInfo(@PathVariable("favoriteId") Long favoriteId) {
        return success(collectService.selectCollectByFavoriteId(favoriteId));
    }

    /**
     * 新增用户收藏
     */
    @PreAuthorize("@ss.hasPermi('collect:collect:add')")
    @Log(title = "用户收藏", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Collect collect) {
        return toAjax(collectService.insertCollect(collect));
    }

    /**
     * 修改用户收藏
     */
    @PreAuthorize("@ss.hasPermi('collect:collect:edit')")
    @Log(title = "用户收藏", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Collect collect) {
        return toAjax(collectService.updateCollect(collect));
    }

    /**
     * 删除用户收藏
     */
    @PreAuthorize("@ss.hasPermi('collect:collect:remove')")
    @Log(title = "用户收藏", businessType = BusinessType.DELETE)
    @DeleteMapping("/{favoriteIds}")
    public AjaxResult remove(@PathVariable Long[] favoriteIds) {
        return toAjax(collectService.deleteCollectByFavoriteIds(favoriteIds));
    }
}
